import { ShellUtil } from "./ShellUtil";
import { ChangeLuaCodeUtil } from "./ChangeLuaCodeUtil";
import { EncryptResUtil } from "./EncryptResUtil";
import { Main } from "../Main";
import { DirUtil } from "./DirUtil";
import { ConfigUtil } from "./ConfigUtil";
import * as fs from "fs";

/**
 * 处理res和src工具，为打包（安装包或热更包）做准备
 */
export class ProcessResSrcUtil {
    static exec(plat: string, iosOrAndroid: "ios" | "android", cb: () => void) {

        let path = Main.codePath + "Resources/";
        ShellUtil.exec(iosOrAndroid == "android" ? `
rsync -avz --delete ${path}resPlatform/resAndroid/ ${path}res/ || exit 1
                    `: `
# rsync -avz --delete ${path}resPlatform/resIos/ ${path}res/ || exit 1 # ios暂不用pvr
                    `, () => {
            console.log("复制对应设备资源 ok");
            ChangeLuaCodeUtil.exec(plat, () => {
                console.log("修改lua代码 ok");
                this.removeUnusedLanguage(plat, ()=>{
                    EncryptResUtil.exec(path, () => {
                        console.log("加密src和res ok");
                        cb();
                    });
                });
            });
        });
    }
    /** 删除不用的语言资源 */
    static removeUnusedLanguage(plat: string, cb: () => void) {
        console.log("多余语言资源删除 开始");
        let path = Main.codePath + "Resources/";
        DirUtil.deleteDirChildExcludeSome(path + "res/ccs/font/", true, ConfigUtil.PlatCfg[plat].language, () => {
            console.log("多余语言资源删除 1");
            DirUtil.deleteDirChildExcludeSome(path + "res/ccs/platformRes/", true, ConfigUtil.PlatCfg[plat].language, () => {
                console.log("多余语言资源删除 2");
                DirUtil.deleteDirChildExcludeSome(path + "res/ccs/platformResNoCompress/", true, ConfigUtil.PlatCfg[plat].language, () => {
                    console.log("多余语言资源删除 3");
                    DirUtil.deleteDirChildExcludeSome(path + "res/ccs/bigPic/platformRes/", true, ConfigUtil.PlatCfg[plat].language, () => {
                        console.log("多余语言资源删除 4");
                        let lang2: { [key: string]: true } = {};
                        for (const key in ConfigUtil.PlatCfg[plat].language) {
                            if (ConfigUtil.PlatCfg[plat].language.hasOwnProperty(key)) {
                                lang2[key + ".lua"] = true;
                            }
                        }
                        DirUtil.deleteDirChildExcludeSome(path + "src/app/language/", false, lang2, () => {
                            cb();
                        });
                    });
                });
            });
        });
    }
    static backupResourcesOrUseResources2(cb: () => void) {
        if (fs.existsSync(`${Main.codePath}Resources2`)) {
            ShellUtil.exec(`
rm -rf ${Main.codePath}Resources || exit 1
cp -rf ${Main.codePath}Resources2 ${Main.codePath}Resources || exit 1
            `, cb);
        } else {
            ShellUtil.exec(`
cp -rf ${Main.codePath}Resources ${Main.codePath}Resources2 || exit 1
            `, cb);
        }
    }
}